{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 深度学习入门作业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "logical and\n",
      "epoch 0 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1 [1 2 0 1 -1 0 -1]\n",
      "epoch 0 sample 2 [0 2 -1 1 0 -1 -1]\n",
      "epoch 0 sample 3 [0 1 -2 0 1 1 1]\n",
      "epoch 1 sample 0 [1 2 -1 0 0 0 0]\n",
      "epoch 1 sample 1 [1 2 -1 0 0 0 0]\n",
      "epoch 1 sample 2 [1 2 -1 1 0 -1 -1]\n",
      "epoch 1 sample 3 [1 1 -2 0 1 1 1]\n",
      "epoch 2 sample 0 [2 2 -1 0 0 0 0]\n",
      "epoch 2 sample 1 [2 2 -1 1 -1 0 -1]\n",
      "epoch 2 sample 2 [1 2 -2 0 0 0 0]\n",
      "epoch 2 sample 3 [1 2 -2 1 0 0 0]\n",
      "epoch 3 sample 0 [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 1 [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 2 [1 2 -2 0 0 0 0]\n",
      "epoch 3 sample 3 [1 2 -2 1 0 0 0]\n",
      "logical or\n",
      "epoch 0 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 1 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 1 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 2 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 2 sample 3 [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 3 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 3 sample 3 [1 2 0 1 0 0 0]\n",
      "logical xor\n",
      "epoch 0 sample 0 [1 2 0 0 0 0 0]\n",
      "epoch 0 sample 1 [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 2 [1 2 0 1 0 0 0]\n",
      "epoch 0 sample 3 [1 2 0 1 -1 -1 -1]\n",
      "epoch 1 sample 0 [0 1 -1 0 0 0 0]\n",
      "epoch 1 sample 1 [0 1 -1 0 1 0 1]\n",
      "epoch 1 sample 2 [1 1 0 1 0 0 0]\n",
      "epoch 1 sample 3 [1 1 0 1 -1 -1 -1]\n",
      "epoch 2 sample 0 [0 0 -1 0 0 0 0]\n",
      "epoch 2 sample 1 [0 0 -1 0 1 0 1]\n",
      "epoch 2 sample 2 [1 0 0 0 0 1 1]\n",
      "epoch 2 sample 3 [1 1 1 1 -1 -1 -1]\n",
      "epoch 3 sample 0 [0 0 0 0 0 0 0]\n",
      "epoch 3 sample 1 [0 0 0 0 1 0 1]\n",
      "epoch 3 sample 2 [1 0 1 1 0 0 0]\n",
      "epoch 3 sample 3 [1 0 1 1 -1 -1 -1]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#逻辑与数据\n",
    "samples_and=[\n",
    "    [0,0,0],\n",
    "    [1,0,0],\n",
    "    [0,1,0],\n",
    "    [1,1,1],\n",
    "]\n",
    "\n",
    "#逻辑或数据\n",
    "samples_or=[\n",
    "    [0,0,0],\n",
    "    [1,0,1],\n",
    "    [0,1,1],\n",
    "    [1,1,1],\n",
    "]\n",
    "\n",
    "#逻辑异或数据  相异为1，相同为0\n",
    "samples_xor=[\n",
    "    [0,0,0],\n",
    "    [1,0,1],\n",
    "    [0,1,1],\n",
    "    [1,1,0],\n",
    "]\n",
    "#定义感知器方法\n",
    "def perceptron(samples):\n",
    "    w=np.array([1,2])#创建一行两列的数组\n",
    "    b=0\n",
    "    a=1\n",
    "    \n",
    "    for i in range(4):\n",
    "        for j in range(4):\n",
    "            x=np.array(samples[j][:2])\n",
    "            y=1 if np.dot(w,x)+b>0 else 0\n",
    "            d=np.array(samples[j][2])\n",
    "            \n",
    "            delta_b=a*(d-y)\n",
    "            delta_w=a*(d-y)*x\n",
    "            \n",
    "            print('epoch {} sample {} [{} {} {} {} {} {} {}]'.format(\n",
    "            i,j,w[0],w[1],b,y,delta_w[0],delta_w[1],delta_b))\n",
    "            w=w+delta_w\n",
    "            b=b+delta_b\n",
    "            \n",
    "            \n",
    "if __name__=='__main__':\n",
    "    print('logical and')\n",
    "    perceptron(samples_and)\n",
    "    print('logical or')\n",
    "    perceptron(samples_or)\n",
    "    print('logical xor')\n",
    "    perceptron(samples_xor)\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 感知器为什么不能表示“异或”"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1、异或---相异为1，相同为0；\n",
    "2、感知器（单层神经网络）\n",
    "输入信息x与权重w的每个分量分别相乘在相加的和通过阶跃函数输出1或者0\n",
    "使用感知器可以完成一些简单的逻辑操作，例如逻辑与\n",
    "当输入x1,x2分别为1，1时，那么输出output=1;根据这个调整w和bias的值可以设计处相应的感知器；\n",
    "3、如何设计感知器\n",
    "根据相应的逻辑我们可以列出真值表，然后将真值表的数值带入到感知器中，可以求解出一些精确的解（比如另1*w1+1*w2+b=1...）\n",
    "将得出的w1,w2和b带回去则得到一条直线，那么线上面的部分就是大于零的，根据阶跃函数，那么它的输出就为1，那么直线下面所有的点输出都小于零，将真值表中的点画在二维坐标轴上可以看的很清楚\n",
    "\n",
    "4、简单来说\n",
    "感知器就是在平面上画一条线，线的一边是一类，\n",
    "但是面对异或问题，\n",
    "XO\n",
    "OX \n",
    "不能找到一条直线分类，所以感知器（单层神经网络）不能解决非线性问题；\n",
    "所有线性模型都无法解决异或问题"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
